<!DOCTYPE html>
<html>

<head>
    <meta charset="UTF-8">
    <!--edge浏览器H5兼容设置-->
    <meta content="IE=edge" http-equiv="X-UA-Compatible">
    <!--360浏览器H5兼容设置-->
    <meta content="webkit" name="renderer"/>
    <title>学子商城</title>
    <meta content="width=device-width, initial-scale=1" name="viewport">
    <!--导入核心文件-->
    <script src="../bootstrap3/js/holder.js"></script>
    <link href="../bootstrap3/css/bootstrap.css" rel="stylesheet" type="text/css">
    <script src="../bootstrap3/jquery-1.8.3.min.js"></script>
    <script src="../bootstrap3/js/bootstrap.js"></script>
    <!-- 字体图标 -->
    <link href="../bootstrap3/font-awesome-4.7.0/css/font-awesome.css" rel="stylesheet"/>
    <link href="../css/layout.css" rel="stylesheet" type="text/css"/>
    <link href="../css/top.css" rel="stylesheet" type="text/css"/>
    <link href="../css/footer.css" rel="stylesheet" type="text/css"/>
    <link href="../css/search.css" rel="stylesheet" type="text/css"/>
    <link href="../css/imgmove.css" rel="stylesheet" type="text/css"/>
    <script charset="utf-8" src="../js/search.js" type="text/javascript"></script>
    <script charset="utf-8" src="../js/imgmove.js" type="text/javascript"></script>
</head>

<body>
<!--头部-->
<header class="header">
    <!--学子商城logo-->
    <div class="row">
        <div class="col-md-3">
            <a href="index.html">
                <img src="../images/index/stumalllogo.png"/>
            </a>
        </div>
        <!--快捷选项-->
        <div class="col-md-9 top-item">
            <ul class="list-inline pull-right">
                <li><a href="favorites.html"><span class="fa fa-heart"></span>&nbsp;收藏</a></li>
                <li class="li-split">|</li>
                <li><a href="orders.html"><span class="fa fa-file-text"></span>&nbsp;订单</a></li>
                <li class="li-split">|</li>
                <li><a href="cart.html"><span class="fa fa-cart-plus"></span>&nbsp;购物车</a></li>
                <li class="li-split">|</li>
                <li>
                    <!--下列列表按钮 ：管理-->
                    <div class="btn-group">
                        <button class="btn btn-link dropdown-toggle" data-toggle="dropdown" type="button">
                            <span id="top-dropdown-btn"><span class="fa fa-gears"></span>&nbsp;管理 <span
                                    class="caret"></span></span>
                        </button>
                        <ul class="dropdown-menu top-dropdown-ul" role="menu">
                            <li><a href="password.html">修改密码</a></li>
                            <li><a href="userdata.html">个人资料</a></li>
                            <li><a href="upload.html">上传头像</a></li>
                            <li><a href="address.html">收货管理 </a></li>
                        </ul>
                    </div>
                </li>
                <li class="li-split">|</li>
                <li><a href="login.html"><span class="fa fa-user"></span>&nbsp;登录</a></li>
            </ul>
        </div>
    </div>
</header>
<!--导航 -->
<!--分割导航和顶部-->
<div class="row top-nav">
    <div class="col-md-6">
        <ul class="nav nav-pills">
            <li>
                <a href="#"></a>
            </li>
            <li class="active"><a href="index.html"><span class="fa fa-home"></span></a></li>
            <li><a href="#">秒杀</a></li>
            <li><a href="#">优惠券</a></li>
            <li><a href="#">学子VIP</a></li>
            <li><a href="#">外卖</a></li>
            <li><a href="#">超市</a></li>
        </ul>
    </div>
    <div class="col-md-6">
        <form action="search.html" class="form-inline pull-right" method="get" role="form">
            <div class="form-group">
                <input class="form-control" id="search" name="name" placeholder="请输入商品名称进行搜索" type="text">
            </div>
            <button class="btn btn-default btn-sm" type="submit"><span class="fa fa-search"></span></button>
        </form>
    </div>
</div>
<!--头部结束-->
<!--导航结束-->
<div class="container" id="prod-list">
    <div class="col-md-offset-1 col-md-10"><b>{{typeName}}</b>&nbsp;<small>搜索结果：</small></div>
    <div class="col-md-offset-1 col-md-10" v-for="lineData in lineDatas">
        <div class="col-md-3" v-for="prod in lineData">
            <div class="goods-panel">
                <img :src="`..`+prod.image+`collect.png`" class="img-responsive"/>
                <p>¥{{prod.price}}</p>
                <p class="text-row-3"><a :href="`product.html?id=`+prod.id"><small>{{prod.title}}</small></a></p>
                <span>
						<a class="btn btn-default btn-xs add-fav" href="javascript:void(0)"><span
                                class="fa fa-heart-o"></span>加入收藏</a>
                    <!--<a href="cart.html" class="btn btn-default btn-xs add-cart"><span class="fa fa-cart-arrow-down"></span>加入购物车</a>-->
						<a :pid="prod.id" class="btn btn-default btn-xs add-cart" href="javascript:void(0)"><span
                                class="fa fa-cart-arrow-down"></span>加入购物车</a>
						</span>
            </div>
        </div>
    </div>
    <div class="col-md-offset-1 col-md-10">
        <nav align="center" aria-label="Page navigation">
            <ul class="pagination">
                <li :class="currentPage==1?`disabled`:``">
                    <a :href="currentPage==1?`javascript:void(0);`:prePagePath" aria-label="Previous">
                        <span aria-hidden="true">&laquo;</span>
                    </a>
                </li>
                <li :class="page==currentPage?`active`:``" v-for="page in pageCount"><a
                        :href="searchPath+`&currentPage=`+page">{{page}}</a></li>
                <li :class="currentPage==pageCount?`disabled`:``">
                    <a :href="currentPage==pageCount?`javascript:void(0);`:nextPagePath" aria-label="Next">
                        <span aria-hidden="true">&raquo;</span>
                    </a>
                </li>
            </ul>
        </nav>
    </div>
</div>
<!--页脚开始-->
<div class="clearfix"></div>
<footer class="footer">
    <!-- 品质保障，私人定制等-->
    <div class="text-center rights container">
        <div class="col-md-offset-2 col-md-2">
            <span class="fa fa-thumbs-o-up"></span>
            <p>品质保障</p>
        </div>
        <div class="col-md-2">
            <span class="fa fa-address-card-o"></span>
            <p>私人订制</p>
        </div>
        <div class="col-md-2">
            <span class="fa fa-graduation-cap"></span>
            <p>学生特供</p>
        </div>
        <div class="col-md-2">
            <span class="fa fa-bitcoin"></span>
            <p>专属特权</p>
        </div>
    </div>
    <!--联系我们、下载客户端等-->
    <div class="container beforeBottom">
        <div class="col-md-offset-1 col-md-3">
            <div><img alt="" class="footLogo" src="../images/index/stumalllogo.png"/></div>
            <div><img alt="" src="../images/index/footerFont.png"/></div>
        </div>
        <div class="col-md-4 callus text-center">
            <div class="col-md-4">
                <ul>
                    <li>
                        <a href="#">
                            <p>买家帮助</p>
                        </a>
                    </li>
                    <li><a href="#">新手指南</a></li>
                    <li><a href="#">服务保障</a></li>
                    <li><a href="#">常见问题</a></li>
                </ul>
            </div>
            <div class="col-md-4">
                <ul>
                    <li>
                        <a href="#">
                            <p>商家帮助</p>
                        </a>
                    </li>
                    <li><a href="#">商家入驻</a></li>
                    <li><a href="#">商家后台</a></li>
                </ul>
            </div>
            <div class="col-md-4">
                <ul>
                    <li>
                        <a href="#">
                            <p>关于我们</p>
                        </a>
                    </li>
                    <li><a href="#">关于达内</a></li>
                    <li><a href="#">联系我们</a></li>
                    <li>
                        <span class="fa fa-wechat"></span>
                        <span class="fa fa-weibo"></span>
                    </li>
                </ul>
            </div>
        </div>
        <div class="col-md-4">
            <div class="col-md-5">
                <p>学子商城客户端</p>
                <img class="lf" src="../images/index/ios.png">
                <img alt="" class="lf" src="../images/index/android.png"/>
            </div>
            <div class="col-md-6">
                <img src="../images/index/erweima.png">
            </div>
        </div>
    </div>
    <!-- 页面底部-备案号 #footer -->
    <div class="col-md-12 text-center bottom">
        Copyright © 2018 Tedu.cn All Rights Reserved 京ICP备08000853号-56 <a href="http://www.tedu.cn/" target="_blank">达内时代科技集团有限公司</a>
        版权所有
    </div>
</footer>
<script src="../js/vue.js"></script>
<!--页脚结束-->
<script type="text/javascript">
    var queryByTypeUrl = "../product/listAllByCid?cid=[cid]&currentPage=[currentPage]&pageSize=[pageSize]";
    var queryByNameUrl = "../product/findProductByName?name=[name]&currentPage=[currentPage]&pageSize=[pageSize]";

    var pageSize = 12;
    var cid = 163;
    var currentPage = 1;
    var map = getPathParams();

    $(function () {
        if (map.get("currentPage") != undefined) {
            currentPage = map.get("currentPage");
        }

        if (map.get("typeid") != undefined) {
            cid = map.get("typeid");
            queryByType();
        } else if (map.get("name") != undefined) {
            queryByName();
        }

    });

    function initCartButton() {
        let addCartUrl = "../cart/save"
        $(".add-cart").bind("click", function () {
            let num = 1;
            let pid = $(this).attr("pid");
            $.ajax({
                url: addCartUrl,
                type: 'POST',
                data: {
                    pid: pid,
                    num: num
                },
                headers: {
                    'AccessToken': window.localStorage.getItem('AccessToken')
                },
                success: function (result) {
                    if (result.code === 1000) {
                        alert("购物车添加成功");
                    } else {
                        alert(result.msg);
                    }
                }
            });
        });
    }

    function queryByName() {
        var typeName = "";
        var name = "";
        if (map.get("name") != undefined) {
            name = map.get("name");
        }

        var url = queryByNameUrl.replace("[name]", name)
            .replace("[currentPage]", currentPage)
            .replace("[pageSize]", pageSize);
        $.get(url, function (result) {
            if (result.code === 1000) {
                // 获取jsonResult中的pageRecord中的data
                var data = result.data.data;
                var lineDataArray = []; // 行数据的数组，元素是lineData
                var lineData = []; // 数据数组，元素是prod对象，表示一行的4条记录
                var counter = 0; // 计数器
                for (var index in data) { // 便利查询到的数据
                    lineData.push(data[index]);
                    counter++; // 计数器+1
                    if (counter % 4 == 0) { // 已添加4条记录
                        lineDataArray.push(lineData); // 将一行记录添加到数组中
                        lineData = []; // 清空容器
                    }
                }
                // 处理最后一行记录
                if (lineData.length != 0) { // lineData中有未添加的记录
                    lineDataArray.push(lineData);
                }
                new Vue({
                    el: '#prod-list',
                    data: {
                        lineDatas: lineDataArray,
                        pageCount: result.data.pageCount,
                        name: name,
                        currentPage: currentPage,
                        nextPagePath: "search.html?name=" + name + "&currentPage=" + (parseInt(currentPage) + 1),
                        prePagePath: "search.html?name=" + name + "&currentPage=" + (parseInt(currentPage) - 1),
                        typeName: typeName,
                        searchPath: "search.html?name=" + name
                    }
                });
            } else {
                alert(result.msg);
                $("#prod-list").empty();
                $("#prod-list").html("<div class='col-md-offset-1 col-md-10'><img src='../images/not_found.jpg' class='img-responsive center-block'></div>");
            }
            initCartButton();
        });
    }

    function queryByType() {
        var typeName = "";
        if (map.get("typename") != undefined) {
            typeName = unescape(map.get("typename"));
        }

        var url = queryByTypeUrl.replace("[cid]", cid)
            .replace("[currentPage]", currentPage)
            .replace("[pageSize]", pageSize);
        $.get(url, function (result) {
            if (result.code === 1000) {
                // 获取jsonResult中的pageRecord中的data
                var data = result.data.data;
                var lineDataArray = []; // 行数据的数组，元素是lineData
                var lineData = []; // 数据数组，元素是prod对象，表示一行的4条记录
                var counter = 0; // 计数器
                for (var index in data) { // 便利查询到的数据
                    lineData.push(data[index]);
                    counter++; // 计数器+1
                    if (counter % 4 === 0) { // 已添加4条记录
                        lineDataArray.push(lineData); // 将一行记录添加到数组中
                        lineData = []; // 清空容器
                    }
                }
                // 处理最后一行记录
                if (lineData.length !== 0) { // lineData中有未添加的记录
                    lineDataArray.push(lineData);
                }
                new Vue({
                    el: '#prod-list',
                    data: {
                        lineDatas: lineDataArray,
                        pageCount: result.data.pageCount,
                        typeid: cid,
                        currentPage: currentPage,
                        nextPagePath: "search.html?typeid=" + cid + "&currentPage=" + (parseInt(currentPage) + 1),
                        prePagePath: "search.html?typeid=" + cid + "&currentPage=" + (parseInt(currentPage) - 1),
                        typeName: typeName,
                        searchPath: "search.html?typeid=" + cid
                    }
                });
            } else {
                alert(result.msg);
                $("#prod-list").empty();
                $("#prod-list").html("<div class='col-md-offset-1 col-md-10'><img src='../images/not_found.jpg' class='img-responsive center-block'></div>");
            }
            initCartButton();
        });

    }


    // 获取地址栏中所有参数构成的Map集合
    function getPathParams() {
        var map = new Map();
        // 获取地址栏中的id参数
        var path = window.location.href;
        var index = path.lastIndexOf("?");
        if (index != -1) {
            var params = path.substring(index + 1);
            var array = params.split("&");
            if (array.length > 0) {
                for (var index in array) {
                    var arr = array[index].split("=");
                    // 将一组参数存入map集合
                    map.set(arr[0], arr[1]);
                }
            }
        }
        return map;
    }
</script>
</body>

</html>